﻿using System;
using System.IO;
using Core;
using Xunit;

namespace UnitTest
{
        public class PostUnitTest : TestingBase
        {
            
            private const string POST_APP_DATA_LOCATION = @"D:/blog/App_Data";

            #region Parse
            [Fact]
            public void ParsePosts_ExpectPostsLoaded()
            {
                Post post = this.GetParsedXmlProvider().Post;
                Assert.Equal(7, post.PostItems.Count);
            }
            [Fact]
            public void ParsePosts_ExpectPostItemValueLoaded()
            {
                Post post = this.GetParsedXmlProvider().Post;
                PostItem pi = post.PostItems[0];

                Assert.Equal("Milk", pi.Title);
                Assert.Equal(new DateTime(2011, 03, 18, 15, 15, 30), pi.PubDate);
                Assert.True(pi.Content.Contains("<strong>Milk</strong> is a white liquid produced"));
                Assert.True(pi.IsPublished);
                Assert.True(pi.IsCommentsEnabled);
            }

            [Fact]
            public void ParsePostMultipleCategories_ExpectLoadsTwoCategories()
            {
                Post post = this.GetParsedXmlProvider().Post;

                PostItem postItem = post.PostItems[2];
                Assert.Equal(2, postItem.CategoryWpNiceNameCollection.Count);
                Assert.Equal("honey", postItem.CategoryWpNiceNameCollection[0]);
            }

            [Fact]
            public void ParsePostCategoryId_ShouldBeNull()
            {
                Post post = this.GetParsedXmlProvider().Post;
                Assert.Null(post.PostItems[0].CategoryIdCollection);
            }

            [Fact]
            public void ParsePages_ExpectPagesLoaded()
            {
                Post post = this.GetParsedXmlProvider().Post;
                Assert.Equal(1, post.PageItems.Count);
                Assert.Equal("Milk", post.PostItems[0].Title);
            }

            [Fact]
            public void ParseTags_ExpectTagsLoaded()
            {
                Post post = this.GetParsedXmlProvider().Post;

                PostItem postItem = post.PostItems[4];

                Assert.Equal(3, postItem.Tags.Count);
                Assert.Equal("bank", postItem.Tags[0]);
                Assert.Equal("money", postItem.Tags[1]);
                Assert.Equal("grow", postItem.Tags[2]);
            }

            [Fact]
            public void ParseIncludeDrafts_ExpectDraftPosts()
            {
                Post post = this.GetParsedXmlProvider(new PostOption { IncludeDraft = true }).Post;

                Console.WriteLine(post.PostItems[0].Title);
                Assert.Equal("Lorem ipsum", post.PostItems[0].Title);
            }

            [Fact]
            public void ParseIncludeDrafts_ExpectPublishAndDraftLoaded()
            {
                Post post = this.GetParsedXmlProvider(new PostOption { IncludeDraft = true }).Post;

                Assert.Equal(8, post.PostItems.Count);
            }

            [Fact]
            public void ParseComments_ExpectCommentsLoaded()
            {
                Post post = this.GetParsedXmlProvider().Post;
                Assert.Equal(0, post.PostItems[1].Comments.Count);
                Assert.Equal(5, post.PostItems[4].Comments.Count);

                CommentItem cItem = post.PostItems[4].Comments[0];
                Assert.True(cItem.Id != Guid.Empty);
                Assert.Equal("albuexport", cItem.Author);
                Assert.Equal("my@email.com", cItem.Email);
                Assert.Equal("http://albuexport.wordpress.com", cItem.WebSite);
                Assert.Equal("87.85.121.98", cItem.Ip);
                Assert.Equal(new DateTime(2011, 09, 26, 15, 19, 46), cItem.Date);
                Assert.Equal("Test", cItem.Content);
                Assert.True(cItem.Approved);
                Assert.Equal(0, cItem.WpParentId);
                Assert.True(post.PostItems[4].Comments[3].Deleted);
                Assert.True(post.PostItems[4].Comments[4].Spam);
            }
            #endregion

            #region Process
            [Fact]
            public void ProcessPosts_ExpectPropertiesSet()
            {
                Post post = this.GetParsedXmlProvider().Post;

                Assert.True(post.PostItems[0].Id == Guid.Empty);
                Assert.Null(post.PostItems[0].CategoryIdCollection);
                Assert.Null(post.PostItems[0].Slug);

                post.Process();

                Assert.False(post.PostItems[0].Id == Guid.Empty);
                Assert.False(post.PostItems[0].CategoryIdCollection.Count == 0);
                Assert.NotEmpty(post.PostItems[0].Slug);
            }

            [Fact]
            public void ProcessPosts_ExpectCategorySet()
            {
                Post post = this.GetParsedXmlProvider().Post;
                post.Process();
                Assert.Equal(2, post.PostItems[2].CategoryIdCollection.Count); // honey has 2 categs
            }

            [Fact]
            public void ReplaceImageLocation_ExpectHrefImgTagFileLocationReplacement()
            {
                ProviderXml provider = this.GetParsedXmlProvider(
                    new PostOption
                        {
                            BlogUrl = "http://www.my.com",
                            RelImageFolder = "export-image"
                        });
                Post post = provider.Post;
                post.Process();
                string postContent = post.PostItems[6].Content;

                Assert.Contains("<a href=\"http://www.my.com/image.axd?picture=export-image/sunset-0.jpg\"  rel=\"lightbox\">", postContent);
                Assert.Contains("src=\"http://www.my.com/image.axd?picture=export-image/sunset-1.jpg", postContent);
            }

            [Fact]
            public void ReplaceAndUpdateImageLocation_ExpectPostImagesAddedToCollection()
            {
                ProviderXml provider = this.GetParsedXmlProvider(
                    new PostOption
                    {
                        BlogUrl = "http://www.my.com",
                        RelImageFolder = "export-image"
                    });
                Post post = provider.Post;
                post.Process();

                Assert.Equal(2, post.PostImages.Count);
            }

            #endregion

            #region Save
            [Fact]
            public void Save_Categories()
            {
                string categoryFullFileName = PreSaveCategory();

                Assert.False(File.Exists(categoryFullFileName));

                ProviderXml provider = this.GetProviderXmlForSave();
                provider.Process();
                provider.SaveCategories();

                Assert.True(File.Exists(categoryFullFileName));
            }

            [Fact]
            public void Save_Posts()
            {
                string postsLocation = PreSavePosts();

                ProviderXml provider = this.GetProviderXmlForSave();
                provider.Process();
                provider.SavePosts();

                Assert.True(Directory.GetFiles(postsLocation).Length > 0);
            }

            [Fact]
            public void Save_Pages()
            {
                string pageLocation = PreSavePages();
                Assert.True(Directory.GetFiles(pageLocation).Length == 0);

                ProviderXml provider = this.GetProviderXmlForSave();

                provider.Process();
                provider.SavePages();

                Assert.True(Directory.GetFiles(pageLocation).Length > 0);
            }

            #endregion

            #region Integration tests

            [Fact]
            public void IntegrationParseCategory()
            {
                ProviderXml providerXml = GetParsedXmlProvider();

                Assert.Equal(7, providerXml.Post.PostCategory.CategoryItems.Count);
            }

            [Fact]
            public void Integration_SaveAll()
            {
                string categoryFullFileName = PreSaveCategory();
                string postsLocation = PreSavePosts();
                string pageLocation = PreSavePages();

                Assert.False(File.Exists(categoryFullFileName));
                Assert.True(Directory.GetFiles(pageLocation).Length == 0);
                Assert.True(Directory.GetFiles(pageLocation).Length == 0);

                ProviderXml provider = this.GetParsedXmlProvider(
                    new PostOption
                    {
                        AuthorName = "Ruslan",
                        IncludeDraft = true,
                        BlogUrl = "http://www.my.com",
                        LocalImageFolder = "export-image",
                        PostAppDataLocation = POST_APP_DATA_LOCATION
                    });
                provider.Process();
                provider.Save();

                Assert.True(File.Exists(categoryFullFileName));
                Assert.True(Directory.GetFiles(postsLocation).Length > 0);
                Assert.True(Directory.GetFiles(pageLocation).Length > 0);
            }


            #endregion

            #region Helper methods

            public ProviderXml GetParsedXmlProvider()
            {
                return this.GetParsedXmlProvider(new PostOption());
            }

            public ProviderXml GetParsedXmlProvider(PostOption postOption)
            {
                ProviderXml xmlProvider = new ProviderXml(postOption);
                xmlProvider.Parse(WP_EXPORT_XML_FILE_NAME);

                return xmlProvider;
            }

            private ProviderXml GetProviderXmlForSave()
            {
                return this.GetParsedXmlProvider(
                    new PostOption
                        {
                            AuthorName = "Ruslan",
                            BlogUrl = "http://www.my.com",
                            LocalImageFolder = "export-image",
                            PostAppDataLocation = POST_APP_DATA_LOCATION
                        });
            }

            private static string PreSaveCategory()
            {
                string categoryFullFileName = Path.Combine(POST_APP_DATA_LOCATION, "categories.xml");

                if (File.Exists(categoryFullFileName))
                {
                    File.Delete(categoryFullFileName);
                }
                return categoryFullFileName;
            }

            private static string PreSavePosts()
            {
                string postsLocation = Path.Combine(POST_APP_DATA_LOCATION, "posts");
                foreach (string file in Directory.GetFiles(postsLocation))
                {
                    File.Delete(file);
                }
                return postsLocation;
            }

            private static string PreSavePages()
            {
                string pageLocation = Path.Combine(POST_APP_DATA_LOCATION, "pages");
                foreach (string file in Directory.GetFiles(pageLocation))
                {
                    File.Delete(file);
                }

                return pageLocation;
            }

            #endregion
        }
}